<template>
	<view>
		<view style="width: 100%;height: 490rpx;">
			<image class="u-skeleton-fillet" style="width: 100%;height: 100%;" :src="order.homepageImg"></image>
		</view>
		<!-- <view class="padding-sm bgs"> -->
		<!-- <view class=" padding flex justify-between bg radius u-skeleton-fillet"> -->
		<!-- <u-avatar :src="order.avatar?order.avatar:'../../../static/logo.png'" mode="square"></u-avatar> -->
		<!-- <view class="flex-sub">
					<view class=" flex justify-between align-center">
						<view class="text-xxl" style="color: #557EFD;">
							<text class="text-df ">￥</text>{{ order.money}}<text
								style="font-size: 28rpx;color: #000000;">/{{order.unit}}<text class="margin-left-xs text-sm">会员价：￥{{ order.memberMoney}}</text>/{{order.unit}}</text>
							<text class=""
								style="color: #999999;font-size: 25rpx;margin-left: 17rpx;">最低{{order.minNum}}{{order.unit}}</text>
						</view>
						<view style="color: #557EFD;background-color: #E1E8FF;font-size: 25rpx;padding: 8upx 10upx;">
							<text>{{order.region?order.region:'不限地区'}}</text>
						</view>

					</view>
					<view class="text-lg margin-top-sm margin-bottom-sm u-line-1" style="width: 600rpx;">
						{{order.myLevel}}
					</view>
					<view class="margin-top-xs ">
						<text class="padding-xs margin-right-xs text-sm"
							style="color: #557EFD;background-color: #E1E8FF;"
							v-for="(item,index) in order.gameName">{{item}}</text>

					</view>
					<view class="flex align-center margin-tb-sm" v-if="order.detailadd"
						@tap="bindGps(order.latitude,order.longitude,order.detailadd)">
						<u-icon name="map"></u-icon>
						<view class="margin-left-xs text-cut addbox">
							{{order.detailadd}}
						</view>
					</view>
				</view> -->
		<!-- </view> -->

		<!-- <view class="margin-top-sm bg radius u-skeleton-fillet" v-if="order.detailsImg"> -->
		<!-- <view class="text-center padding">服务详情</view> -->
		<!-- <image src="../../../static/images/detail.png" style="width: 100%;"></image> -->
		<!-- <image v-for="(item,index) in order.detailsImg" :key="index" :src="item" style="width: 100%;"
					mode="widthFix"></image> -->
		<!-- <view class="padding-lr padding-bottom">
					<u-parse class='margin-top' :html="order.detailsImg"></u-parse>
				</view> -->
		<!-- <view style="max-width:100%;padding-bottom: 20rpx;">
					<uParse :content="order.detailsImg" @navigate="navigate"
						style="display:block;width:92%;max-width:92%;background-size: 100%;height:auto;padding: 0 30rpx;">
					</uParse>
				</view> -->
		<!-- </view> -->
		<!-- </view> -->

		<view class="bg padding-lr padding-tb-sm">
			<view class="flex align-center justify-between">
				<view class="text-bold margin-top-sm margin-bottom-sm u-line-1" style="width: 500rpx;font-size: 32upx;">
					{{order.myLevel}}
				</view>
				<view v-if="order.count">已服务{{order.count}}人</view>
			</view>

			<!-- <view class="flex align-center">
				<view class="radius margin-right-xs "
					style="color: #FF2D01;background-color: #FBEBF0;font-size: 25rpx;padding: 8upx 10upx;">
					<text>{{order.region?order.region:'不限地区'}}</text>
				</view>
				<view class=" ">
					<text class="padding-xs margin-right-xs  radius"
						style="color: #FF2D01;background-color: #FBEBF0;font-size: 25upx;"
						v-for="(item,index) in order.gameName" :key="index">{{item}}</text>
		
				</view>
			</view> -->
			<view class="text-gray text-sm">
				<text>
					{{order.serviceMethod === '1' ? '上门服务 ':'到店服务 '}}
				</text>
				<text v-if="order.restrictedArea">
					服务范围: {{queryCitiesByDistrictCodes(order.restrictedArea)}}
				</text>
			</view>

			<view class=" flex justify-between align-center margin-top-sm">
				<view class="text-xxl" style="color: #FF2D01;" v-if=" order.memberMoney">
					{{isVip? order.memberMoney :order.money}}<text style="font-size: 28rpx;">元/{{order.unit}}</text>

					<text v-if="isVip" style="font-size: 26rpx;margin-left: 10rpx;">会员价</text>
				</view>
				<view v-if=" order.minNum">
					{{order.minNum}}{{order.unit}}起购
				</view>

			</view>

			<view class="vipbox" v-if="!isVip&&isVipkg!='否'&&order.memberMoney">
				<view>会员价
					<text class="text-bold">￥</text>
					<text style="font-size: 34rpx;font-weight: bold;">{{order.memberMoney}}</text>
				</view>
				<view class="flex align-center text-sm" @click="goVip()">
					立即开通
					<image src="../../../static/images/vipright.png"
						style="width: 15rpx;height: 25rpx;margin-left:10rpx;"></image>
				</view>
			</view>

			<view class="flex align-center align-center margin-tb-sm padding-lr padding-tb-xs " v-if="order.detailadd"
				@tap="bindGps(order.latitude,order.longitude,order.detailadd)"
				style="background: #FFF8F4;border: 1rpx solid #E0D0A7;border-radius: 14upx;">
				<view class="flex align-center">
					<image src="../../../static/images/index/xqadd.png" style="width: 27upx;height: 37upx;"></image>
					<view class="margin-left-xs text-cut addbox">
						{{order.detailadd}}
					</view>
				</view>
				<view>
					<image src="../../../static/images/my/right.png" style="width: 11rpx;height: 18rpx;"></image>
				</view>
			</view>

		</view>
		
		<view class="padding-sm bgs u-skeleton-fillet">
			<view class="margin-top-sm bg radius u-skeleton-fillet padding-bottom" v-if="order.detailsImg">
				<view class="text-center padding">服务详情</view>
				<!-- <view v-for="(item,index) in order.detailsImg" :key="index"
					@click="saveImg(order.detailsImg,index)">
					<image :src="item" style="width: 100%;" mode="widthFix"></image>
				</view> -->
		
				<u-parse class='margin-top' :html="order.detailsImg"></u-parse>
			</view>
		
		</view>


		<view class="text-white flex justify-between cu-bar foot bg padding-lr" v-if="myId != order.userId">
			<view @click="follow" class="padding-lr" style="width: 120rpx;">
				<!-- <image src="../../../static/images/index/guanzhu.png" v-if="!isFollow"
					style="width: 49rpx;height: 43rpx;"></image> -->
				<!-- <image src="../../../static/images/index/guanzhu1.png" v-else style="width: 49rpx;height: 43rpx;">
				</image> -->
				<!-- <view style="font-size: 22upx;margin-top: 8upx;">关注</view> -->
			</view>
			<view @click="goMsg" class="padding-lr" style="width: 120rpx;">
				<image src="../../../static/images/index/im.png" style="width: 49rpx;height: 43rpx;"></image>
				<view style="font-size: 22upx;margin-top: 8upx;">聊天</view>
			</view>
			<view class="flex-sub">
				<u-button :custom-style="customStyle" @click="goNav()" shape="circle" :hair-line="false">立即下单</u-button>
			</view>
		</view>
		<!-- <u-skeleton :loading="loading" :animation="true" elColor='#1E1F31' bgColor='#ffffff' ></u-skeleton> -->
	</view>
</template>

<script>
	import address from '@/utils/area.js'
	export default {
		data() {
			return {
				loading: true, // 是否显示骨架屏组件
				customStyle: {
					width: '400upx',
					color: '#FFFFFF',
					background: "#2087fe",
					border: 0
				},
				id: '',
				orderDet: {},
				page: 1,
				limit: 10,
				order: {},
				commentList: [],
				isFollow: false,
				myId: '',
				isVip: false,
				AUDIO: uni.createInnerAudioContext(),
				isPlay: false
			}
		},
		computed: {
			queryCitiesByDistrictCodes() {
				let regions = address.data;
				return function(fwJson) {
					let districtCodes = JSON.parse(fwJson);
					return regions.reduce((result, province) => {
						const matchedCities = province.children.reduce((cities, city) => {
							const matchedDistricts = city.children.filter(district => districtCodes
								.includes(district.code));
							if (matchedDistricts.length === city.children.length) {
								cities.push(`${city.name}(全部范围)`);
							} else if (matchedDistricts.length > 0) {
								cities.push(
									`${city.name}(${matchedDistricts.map(d => d.name).join('，')})`);
							}
							return cities;
						}, []);

						if (matchedCities.length > 0) {
							result.push(`${province.name}[${matchedCities.join('，')}]`);
						}
						return result;
					}, []).join(';');
				}
			},
		},
		onLoad(option) {
			this.id = option.id
			this.getDet()
			this.getOrderComment()
			this.myId = uni.getStorageSync('userId')
			this.isVip = uni.getStorageSync('isVIP')
			console.log(this.isVip)
		},
		onReady() {
			this.AUDIO.onEnded(function(res) {
				this.isPlay = false;
			});
		},
		methods: {
			navigate(href, e) {
				// #ifdef H5
				window.location.href = href;
				//#endif

				//#ifdef APP-PLUS
				setTimeout(function() {
					plus.runtime.openURL(href);
				}, 500);
				// #endif
			},
			// 详情
			getDet() {
				this.$Request.get("/app/orderTaking/queryTakingDetails", {
					id: this.id,
				}).then(res => {
					if (res.code == 0) {
						this.order = res.data
						this.order.gameName = res.data.gameName.split(',')
						this.order.detailsImg = res.data.detailsImg
						// if (this.order.region) {

						// 	let region = this.order.region.split(",");
						// 	this.order.region = region[1]
						// } else {
						// 	this.order.region = '不限地区'
						// }
						this.selectFollow()
					} else {

					}
				});
			},
			// 评论
			getOrderComment() {
				this.$Request.get("/app/takingComment/selectOrderTakingComment", {
					id: this.id,
					page: this.page,
					limit: this.limit
				}).then(res => {
					if (res.code == 0) {
						this.commentList = [...this.commentList, ...res.data.list]
					}
				});
			},
			// 是否关注
			selectFollow() {
				this.$Request.get("/app/userFollow/selectFollowUser", {
					followUserId: this.order.userId
				}).then(res => {
					if (res.data == true) {
						this.isFollow = true
					} else {
						this.isFollow = false
					}
					this.loading = false;
				});
			},
			playVoice() {
				console.log(this.isPlay)
				this.AUDIO.src = this.order.voiceIntroduce;
				if (this.isPlay) {
					this.AUDIO.stop();
				} else {
					this.AUDIO.play();
				}
				this.isPlay = !this.isPlay;
			},
			goNav() {
				uni.navigateTo({
					url: "/pages/index/game/orderDet?id=" + this.id
				})
			},
			goMsg() {
				let data = {
					userId: this.myId,
					focusedUserId: this.order.userId,
					userType: 2
				}
				this.$Request.postJson('/app/chat/insertChatConversation', data).then(res => {
					if (res.code == 0) {
						let id = this.order.userId == res.data.userId ? res.data.focusedUserId : this.order.userId
						uni.navigateTo({
							url: '/pages/msg/im?chatConversationId=' + res.data.chatConversationId +
								'&byUserId=' + id
						})
					}
				})
			},
			// 关注
			follow() {
				let that = this
				that.$Request.get("/app/userFollow/insert", {
					followUserId: that.order.userId
				}).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					setTimeout(function() {
						that.selectFollow()
					}, 500)
				});
			}
		}

	}
</script>

<style>
	page {
		background-color: #F5F5F5;
	}

	.bg {
		background: #FFFFFF;
	}

	.line_s {
		display: inline-flex;
		width: 10rpx;
		height: 10rpx;
		background: #1AD566;
		border-radius: 50%;
		margin-right: 10rpx;
	}

	.line_x {
		display: inline-flex;
		width: 10rpx;
		height: 10rpx;
		background: #000000;
		border-radius: 50%;
		margin-right: 10rpx;
	}

	.addbox {
		width: 600upx;
	}

	.vipbox {
		margin-top: 10rpx;
		background: linear-gradient(to right, #3e3e47 5%, #26252b 90%);
		color: #ffe5c3;
		border-radius: 12rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 15rpx 30rpx;
	}
</style>